home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / toolkit / vbof_v11 / vbofdata.cls < prev    next >
Text File  |  1996-03-03  |  19KB  |  710 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "VBOFDataWrapper"
  6. Attribute VB_Creatable = True
  7. Attribute VB_Exposed = True
  8. Option Explicit
  9.  
  10. ' (c) Copyright 1995 Ken Fitzpatrick
  11. '     All Rights Reserved
  12. '     Cannot be distributed or sold without permission
  13. '
  14. ' VBOFDataWrapper is a supplemental GUI
  15. '   Control Wrapper for Microsoft Visual Basic 4.0.
  16. '   It is valid only in conjunction with the
  17. '   following Classes Modules:
  18. '       VBOFCollection
  19. '       VBOFObjectLink
  20. '       VBOFObjectManager
  21.  
  22. ' VBOFDataWrapper is a wrapper class for
  23. '   providing automatic interfacing between a
  24. '   RecordSet VB control and an underlying
  25. '   VBOFCollection
  26.  
  27. Private pvtVBOFObjectManager As VBOFObjectManager
  28. Private pvtCollection As VBOFCollection
  29. Private pvtDataControl As Variant
  30. Private pvtSupportedTypeNames As String
  31. Private pvtDataControlSupportedTypeNames As String
  32. Private pvtCollectionSupportedTypeNames As String
  33. Private pvtPreviousDataControlActionCode As Long
  34.  
  35. Public ObjectID As Long
  36.  
  37. Public Property Get AbsolutePositionObject() As Variant
  38. ' Returns the object at the AbsolutionPosition (+ 1)
  39. '   of the underlying RecordSet
  40.     
  41.     Dim tempLong As Long
  42.     
  43.     On Local Error Resume Next
  44.  
  45.     tempLong = AbsolutePosition
  46.  
  47.     If tempLong >= 0 Then
  48.         Set AbsolutePositionObject = _
  49.             pvtCollection.Item _
  50.                 (tempLong + 1)
  51.     Else
  52.         Set AbsolutePositionObject = _
  53.             Nothing
  54.     End If
  55. End Property
  56.  
  57.  
  58. Public Property Set AbsolutePositionObject(Object As Variant)
  59. ' Sets the AbsolutionPosition (+ 1) of the
  60. '   underlying RecordSet to correspond to the
  61. '   object
  62.  
  63.     Dim tempLong As Long
  64.     
  65.     On Local Error Resume Next
  66.     
  67.     tempLong = _
  68.         pvtCollection.CollectionIndex _
  69.             (Item:=Object)
  70.  
  71.     If tempLong > 0 Then
  72.         AbsolutePosition = tempLong - 1
  73.     End If
  74. End Property
  75.  
  76.  
  77.  
  78. Public Function CloseRecordSet() As Long
  79. ' Closes the underlying RecordSet
  80.  
  81. ' bullet-proofing
  82.     If Not pvtIsFullyInitialized _
  83.         (Verbose:=True) _
  84.     Then
  85.         CloseRecordSet = -1
  86.         Exit Function
  87.     End If
  88.     
  89.     CloseRecordSet = _
  90.         pvtCollection. _
  91.             pvtCloseRecordSet()
  92. End Function
  93. Public Property Get Clone() As RecordSet
  94. ' Returns a cloned RecordSet of the underlying
  95. '   RecordSet object
  96.  
  97. ' bullet-proofing
  98.     If Not pvtIsFullyInitialized _
  99.         (Verbose:=True) _
  100.     Then
  101.         Set Clone = Nothing
  102.         Exit Property
  103.     End If
  104.     
  105.     Set Clone = _
  106.         pvtCollection.pvtCloneRecordSet()
  107. End Property
  108.  
  109.  
  110. Public Property Get Collection() As Variant
  111. ' Returns my VBOFCollection object
  112.  
  113.     Set Collection = pvtCollection
  114. End Property
  115. Public Property Set Collection(Collection As Variant)
  116.     
  117.     If Collection Is Nothing Then
  118.         Set pvtCollection = Nothing
  119.         Exit Property
  120.     End If
  121.     
  122.     pvtVerifyCollection _
  123.         Collection:=Collection, _
  124.         Verbose:=True
  125.  
  126. '    Set pvtCollection = Collection
  127. End Property
  128.  
  129. Public Property Get DataControl() As Variant
  130.     Set DataControl = pvtDataControl
  131. End Property
  132.  
  133. Public Property Set DataControl(DataControl As Variant)
  134.     pvtVerifyDataControl _
  135.         DataControl:=DataControl, _
  136.         Verbose:=True
  137. End Property
  138.  
  139. Public Property Get ObjectManager() As VBOFObjectManager
  140. ' Return my reference to the VBOFObjectManager
  141.     
  142.     Set ObjectManager = pvtVBOFObjectManager
  143. End Property
  144. Public Property Set ObjectManager(anObjectManager As VBOFObjectManager)
  145. ' Set my reference to the VBOFObjectManager
  146.     
  147.     Set pvtVBOFObjectManager = anObjectManager
  148. End Property
  149.  
  150. Public Function MoveToItem( _
  151.     Optional Item As Variant) As Variant
  152. ' Positions the underlying RecordSet to the
  153. '   specifed Item and returns the Item
  154.  
  155. ' bullet-proofing
  156.     If Not pvtIsFullyInitialized _
  157.         (Verbose:=True) _
  158.     Then
  159.         Set MoveToItem = Nothing
  160.         Exit Function
  161.     End If
  162.  
  163.     Set MoveToItem = _
  164.         pvtCollection. _
  165.             pvtRecordSetPositionToItem _
  166.                     (Item:=Item)
  167. End Function
  168.  
  169. Public Function MoveToObject( _
  170.     Optional Object As Variant) As Variant
  171. ' Positions the underlying RecordSet to the
  172. '   specifed Object and returns the Object
  173.  
  174.     Me.MoveToItem _
  175.        Item:=Object
  176. End Function
  177.  
  178. Private Sub pvtRefreshDataControl()
  179. ' Set the DataControl.RecordSet to the
  180. '   Collection.RecordSet
  181.  
  182.     Dim tempLong As Long
  183.  
  184.     On Local Error Resume Next
  185.  
  186.     If Not pvtIsFullyInitialized() _
  187.     Then
  188.         Exit Sub
  189.     End If
  190.     
  191.     tempLong = pvtDataControl.RecordSet.AbsolutePosition
  192.     pvtCollection.RecordSet.AbsolutePosition = tempLong
  193.     
  194.     Set pvtDataControl.RecordSet = _
  195.         pvtCollection.RecordSet
  196.  
  197. End Sub
  198.  
  199. Private Function pvtVerifyDataControl(Optional DataControl As Variant, Optional Verbose As Variant) As Boolean
  200.     pvtVerifyDataControl = _
  201.         ObjectManager. _
  202.             pvtWrapperVerifyControl( _
  203.                 Control:=DataControl, _
  204.                 pvtControl:=pvtDataControl, _
  205.                 Verbose:=Verbose)
  206. End Function
  207.  
  208. Private Function pvtVerifyCollection(Optional Collection As Variant, Optional Verbose As Variant) As Boolean
  209.     pvtVerifyCollection = _
  210.         ObjectManager. _
  211.             pvtWrapperVerifyCollection( _
  212.                 Collection:=Collection, _
  213.                 pvtCollection:=pvtCollection, _
  214.                 Verbose:=Verbose, _
  215.                 WrapperName:="Data")
  216. End Function
  217.  
  218.  
  219. Private Function pvtErrorMessage(Optional ErrorMessage As Variant) As Long
  220.     pvtErrorMessage = _
  221.         pvtVBOFObjectManager.DisplayErrorMessage _
  222.             (ErrorMessage)
  223. End Function
  224. Private Function pvtIsFullyInitialized(Optional Collection As Variant, Optional DataControl As Variant, Optional Verbose As Variant) As Boolean
  225.     
  226.     If Not pvtVerifyCollection( _
  227.         Collection:=Collection, _
  228.         Verbose:=Verbose) _
  229.     Then
  230.         pvtIsFullyInitialized = False
  231.         Exit Function
  232.     End If
  233.     
  234.     If Not pvtVerifyDataControl( _
  235.         DataControl:=DataControl, _
  236.         Verbose:=Verbose) _
  237.     Then
  238.         pvtIsFullyInitialized = False
  239.         Exit Function
  240.     End If
  241.  
  242.     pvtIsFullyInitialized = True
  243. End Function
  244.  
  245.  
  246. Private Function pvtUseDataControl(Optional DataControlParm As Variant, Optional Verbose As Variant) As Variant
  247.     Set pvtUseDataControl = _
  248.         ObjectManager. _
  249.             pvtWrapperUseControl( _
  250.                 ControlParm:=DataControlParm, _
  251.                 pvtControl:=pvtDataControl, _
  252.                 SupportedNames:=pvtDataControlSupportedTypeNames, _
  253.                 Verbose:=Verbose, _
  254.                 WrapperName:="Data")
  255. End Function
  256.  
  257. Private Function pvtUseCollection(Optional CollectionParm As Variant, Optional Verbose As Variant) As Variant
  258.     Set pvtUseCollection = _
  259.         ObjectManager. _
  260.             pvtWrapperUseCollection( _
  261.                 CollectionParm:=CollectionParm, _
  262.                 pvtCollection:=pvtCollection, _
  263.                 Verbose:=Verbose, _
  264.                 WrapperName:="Data")
  265. End Function
  266.  
  267.  
  268. Public Function Rebind( _
  269.     Optional Collection As Variant, _
  270.     Optional DataControl As Variant) As Variant
  271. ' Rebinds the Wrapper to a Collection or DataControl
  272. '   after having changed the assignment of either.
  273. '   For example, in the following scenario, the
  274. '   VBOFDataWrapper must be rebound because
  275. '   the VBOFCollection has been significantly altered:
  276. '
  277. '   Dim pvtAddresses as VBOFCollection
  278. '   Dim pvtPerson as Person
  279. '   Dim MyDataWrapper as VBOFDataWrapper
  280. '   Set MyDataWrapper = _
  281. '       ObjectManager.NewVBOFDataWrapper ( _
  282. '           Collection:=pvtAddresses, _
  283. '           DataControl:=MyDataControl)
  284. '
  285. ' the following line alters the state of the data
  286. ' in-effect at the time of the above binding
  287. '   Set pvtAddresses = pvtPerson.Addresses
  288. ' rebind the Wrapper
  289. '   MyDataWrapper.Rebind _
  290. '           Collection:=pvtAddresses
  291.     
  292. ' bullet-proofing
  293.     If Not IsMissing(Collection) Then
  294.         If TypeName(Collection) <> _
  295.             "VBOFCollection" _
  296.         Then
  297.             pvtErrorMessage TypeName(Me) & " cannot process the '.Rebind' method because the 'Collection:=' parameter is not a VBOFCollection."
  298.             Rebind = False
  299.             Exit Function
  300.         End If
  301.     End If
  302.     If Not IsMissing(DataControl) Then
  303.         If InStr(pvtDataControlSupportedTypeNames, TypeName(pvtDataControl)) = 0 Then
  304.             pvtErrorMessage TypeName(Me) & " cannot process the '.Rebind' method because the 'DataControl:=' parameter is not a Visual Basic DataControl control.  Please use a VBOF Wrapper for the " & TypeName(DataControl) & " control (or request the development of one.)"
  305.             Rebind = False
  306.             Exit Function
  307.         End If
  308.     End If
  309.     If Not pvtIsFullyInitialized( _
  310.         Collection:=Collection, _
  311.         DataControl:=DataControl, _
  312.         Verbose:=False) _
  313.     Then
  314.         Rebind = False
  315.         Exit Function
  316.     End If
  317.  
  318.     pvtRefreshDataControl
  319.     
  320.     Rebind = True
  321. End Function
  322.  
  323. Public Property Get RecordSet() As RecordSet
  324. ' Returns a DataControl-ready RecordSet object
  325. '   which pertains to the collection of objects
  326. '   instantiated and contained within this
  327. '   VBOFCollection
  328.  
  329. ' bullet-proofing
  330.     If Not pvtIsFullyInitialized _
  331.         (Verbose:=True) _
  332.     Then
  333.         Set RecordSet = Nothing
  334.         Exit Property
  335.     End If
  336.     
  337.     Set RecordSet = _
  338.         pvtCollection.RecordSet
  339. End Property
  340.  
  341. Public Property Get AbsolutePosition() As Long
  342. ' Pass-thru to pvtRecordSetMoveToRecordNumber
  343.     
  344.     On Local Error Resume Next
  345.  
  346. ' bullet-proofing
  347.     If Not pvtIsFullyInitialized _
  348.         (Verbose:=True) _
  349.     Then
  350.         AbsolutePosition = -1
  351.         Exit Property
  352.     End If
  353.  
  354.     AbsolutePosition = _
  355.         pvtDataControl. _
  356.             RecordSet.AbsolutePosition
  357. End Property
  358.  
  359. Public Property Let AbsolutePosition(RecordNumber As Long)
  360. ' Pass-thru to pvtRecordSetMoveToRecordNumber
  361.     
  362.     On Local Error Resume Next
  363.  
  364. ' bullet-proofing
  365.     If Not pvtIsFullyInitialized _
  366.         (Verbose:=True) _
  367.     Then
  368.         Exit Property
  369.     End If
  370.  
  371.     pvtDataControl. _
  372.         RecordSet.AbsolutePosition = _
  373.             RecordNumber
  374. End Property
  375.  
  376. Public Property Get EOF() As Boolean
  377. ' Returns a boolean, based on whether or not the
  378. ' underlying RecordSet is positioned at EOF
  379.  
  380. ' bullet-proofing
  381.     If Not pvtIsFullyInitialized _
  382.         (Verbose:=True) _
  383.     Then
  384.         EOF = False
  385.         Exit Property
  386.     End If
  387.     
  388.     EOF = _
  389.         pvtCollection. _
  390.             pvtRecordSetEOF
  391. End Property
  392. Public Function FindFirst( _
  393.     Optional SearchCriteria As Variant) As Variant
  394. ' Searches the underlying RecordSet for the first
  395. '   record meeting the specified criteria
  396. '   and returns the object for that row
  397.  
  398. ' bullet-proofing
  399.     If Not pvtIsFullyInitialized _
  400.         (Verbose:=True) _
  401.     Then
  402.         Set FindFirst = Nothing
  403.         Exit Function
  404.     End If
  405.  
  406.     Set FindFirst = _
  407.         pvtCollection. _
  408.             pvtRecordSetFindFirst _
  409.                 (SearchCriteria:=SearchCriteria)
  410. End Function
  411.  
  412. Public Function FindLast( _
  413.     Optional SearchCriteria As Variant) As Variant
  414. ' Searches the underlying RecordSet for the last
  415. '   record meeting the specified criteria
  416. '   and returns the object for that row
  417.  
  418. ' bullet-proofing
  419.     If Not pvtIsFullyInitialized _
  420.         (Verbose:=True) _
  421.     Then
  422.         Set FindLast = Nothing
  423.         Exit Function
  424.     End If
  425.  
  426.     Set FindLast = _
  427.         pvtCollection. _
  428.             pvtRecordSetFindLast _
  429.                 (SearchCriteria:=SearchCriteria)
  430. End Function
  431.  
  432. Public Function FindPrevious( _
  433.     Optional SearchCriteria As Variant) As Variant
  434. ' Searches the underlying RecordSet for the previous
  435. '   record meeting the specified criteria
  436. '   and returns the object for that row
  437.  
  438. ' bullet-proofing
  439.     If Not pvtIsFullyInitialized _
  440.         (Verbose:=True) _
  441.     Then
  442.         Set FindPrevious = Nothing
  443.         Exit Function
  444.     End If
  445.  
  446.     Set FindPrevious = _
  447.         pvtCollection. _
  448.             pvtRecordSetFindPrevious _
  449.                 (SearchCriteria:=SearchCriteria)
  450. End Function
  451.  
  452. Public Function FindNext( _
  453.     Optional SearchCriteria As Variant) As Variant
  454. ' Searches the underlying RecordSet for the next
  455. '   record meeting the specified criteria
  456. '   and returns the object for that row
  457.  
  458. ' bullet-proofing
  459.     If Not pvtIsFullyInitialized _
  460.         (Verbose:=True) _
  461.     Then
  462.         Set FindNext = Nothing
  463.         Exit Function
  464.     End If
  465.  
  466.     Set FindNext = _
  467.         pvtCollection. _
  468.             pvtRecordSetFindNext _
  469.                 (SearchCriteria:=SearchCriteria)
  470. End Function
  471.  
  472. Public Function MoveFirst() As Variant
  473. ' Moves the underlying RecordSet to the first record
  474. '   and returns the object for that row
  475.  
  476. ' bullet-proofing
  477.     If Not pvtIsFullyInitialized _
  478.         (Verbose:=True) _
  479.     Then
  480.         Set MoveFirst = Nothing
  481.         Exit Function
  482.     End If
  483.  
  484.     Set MoveFirst = _
  485.         pvtCollection. _
  486.             pvtRecordSetMoveFirst
  487. End Function
  488.  
  489. Public Function MoveLast() As Variant
  490. ' Moves the underlying RecordSet to the Last record
  491. '   and returns the object for that row
  492.  
  493. ' bullet-proofing
  494.     If Not pvtIsFullyInitialized _
  495.         (Verbose:=True) _
  496.     Then
  497.         Set MoveLast = Nothing
  498.         Exit Function
  499.     End If
  500.  
  501.     Set MoveLast = _
  502.         pvtCollection. _
  503.             pvtRecordSetMoveLast
  504. End Function
  505. Public Function MoveToRecordNumber( _
  506.     Optional RecordNumber As Variant) As Variant
  507. ' Moves the underlying RecordSet to the specified
  508. '   record (by number) and returns the object for
  509. '   that row
  510.  
  511. ' bullet-proofing
  512.     If Not pvtIsFullyInitialized _
  513.         (Verbose:=True) _
  514.     Then
  515.         Set MoveToRecordNumber = Nothing
  516.         Exit Function
  517.     End If
  518.  
  519.     Set MoveToRecordNumber = _
  520.         pvtCollection. _
  521.             pvtRecordSetMoveToRecordNumber _
  522.                 (RecordNumber:=RecordNumber)
  523. End Function
  524.  
  525. Public Property Get RecordCount() As Long
  526. ' Returns the RecordCount property of the
  527. ' underlying RecordSet
  528.  
  529. ' bullet-proofing
  530.     If Not pvtIsFullyInitialized _
  531.         (Verbose:=True) _
  532.     Then
  533.         RecordCount = -1
  534.         Exit Property
  535.     End If
  536.     
  537.     RecordCount = _
  538.         pvtCollection. _
  539.             pvtRecordSetRecordCount()
  540. End Property
  541.  
  542. Public Function Refresh( _
  543.     Optional DisplayOnly As Variant) As RecordSet
  544. ' Refresh the DataControl
  545.  
  546. ' Pass thru to pvtRefreshRecordSet()
  547.  
  548. ' bullet-proofing
  549.     If Not pvtIsFullyInitialized _
  550.         (Verbose:=True) _
  551.     Then
  552.         Set Refresh = Nothing
  553.         Exit Function
  554.     End If
  555.  
  556.     pvtRefreshDataControl
  557.     
  558.     If Not IsMissing(DisplayOnly) Then
  559.         If DisplayOnly Then
  560.             Set Refresh = _
  561.                 pvtCollection.RecordSet
  562.         Else
  563.             Set Refresh = _
  564.                 pvtCollection.pvtRecordSetRefresh
  565.         End If
  566.     Else
  567.         Set Refresh = _
  568.             pvtCollection.pvtRecordSetRefresh
  569.     End If
  570. End Function
  571.  
  572. Public Function Sort( _
  573.     Optional SortField As Variant, _
  574.     Optional SortOrder As Variant) As Boolean
  575. ' Sorts the objects in the underlying
  576. '   VBOFCollection according to the field
  577. '   referenced in SortField:= and the sort
  578. '   order referenced in SortOrder:=
  579. ' For additional information, see the VBOF User's
  580. '   Guide
  581. ' Programming example:
  582. '   MyWrapper.Sort _
  583. '       SortField:="FirstName", _
  584. '       SortOrder:="ASC"
  585.         
  586.     Sort = _
  587.         ObjectManager.pvtWrapperSort( _
  588.             Wrapper:=Me, _
  589.             SortField:=SortField, _
  590.             SortOrder:=SortOrder)
  591. End Function
  592.  
  593. Public Function Unbind() As Boolean
  594.  
  595.     Set pvtCollection = Nothing
  596.     Set pvtDataControl = Nothing
  597.     Set pvtVBOFObjectManager = Nothing
  598.  
  599. End Function
  600.  
  601.  
  602. Public Property Get BOF() As Boolean
  603. ' Returns a boolean, based on whether or not the
  604. ' underlying RecordSet is positioned at BOF
  605.  
  606. ' bullet-proofing
  607.     If Not pvtIsFullyInitialized _
  608.         (Verbose:=True) _
  609.     Then
  610.         BOF = False
  611.         Exit Property
  612.     End If
  613.     
  614.     BOF = _
  615.         pvtCollection. _
  616.             pvtRecordSetBOF
  617. End Property
  618.  
  619.  
  620.  
  621. Public Function Validate(Action As Integer, Save As Integer, Optional Sample As Variant, Optional Parent As Variant) As Variant
  622. ' NOT CURRENTLY SUPPORTED
  623. ' Manages the Data1_Validate event procedure for
  624. '   the bound Data control
  625. '
  626. ' Programming example:
  627. '   Private Sub Data1_Validate(Action As Integer, Save As Integer)
  628. '       pvtPersonsDataWrapper. _
  629. '            Validate _
  630. '                Action:=Action, _
  631. '                Save:=Save
  632.  
  633. Exit Function
  634.     Dim tempActionCode As Long
  635.  
  636.     On Local Error Resume Next
  637.     
  638. ' make sure the Action is one that is handled
  639.     If Action = vbDataActionUpdate Then
  640.         If pvtPreviousDataControlActionCode = vbDataActionAddNew Then
  641.             tempActionCode = vbDataActionAddNew
  642.         Else
  643.             tempActionCode = vbDataActionUpdate
  644.         End If
  645.     ElseIf Action = vbDataActionDelete Then
  646.         Action = vbDataActionDelete
  647.     Else
  648.         pvtPreviousDataControlActionCode = Action
  649.         Set Validate = Nothing
  650.         Exit Function
  651.     End If
  652.  
  653. ' bullet-proofing
  654.     If Not pvtIsFullyInitialized _
  655.         (Verbose:=True) _
  656.     Then
  657.         Set FindPrevious = Nothing
  658.         Exit Function
  659.     End If
  660.     If IsMissing(Action) Then
  661.         pvtErrorMessage TypeName(Me) & " cannot process the '.Validate' method because the 'Action:=' parameter is missing."
  662.         Set Validate = Nothing
  663.         Exit Function
  664.     End If
  665.     If IsMissing(Save) Then
  666.         pvtErrorMessage TypeName(Me) & " cannot process the '.Validate' method because the 'Save:=' parameter is missing."
  667.         Set Validate = Nothing
  668.         Exit Function
  669.     End If
  670.     
  671.     Set Validate = _
  672.         pvtCollection. _
  673.             pvtDataValidate( _
  674.                 DataControl:=pvtDataControl, _
  675.                 Action:=tempActionCode, _
  676.                 Save:=Save, _
  677.                 Sample:=Sample, _
  678.                 Parent:=Parent)
  679.     
  680.     pvtCollection.Refresh
  681.     pvtPreviousDataControlActionCode = Action
  682. End Function
  683.  
  684. Private Sub Class_Initialize()
  685.     
  686.     pvtSupportedTypeNames = _
  687.         "RecordSet DynaSet SnapShot"
  688.     pvtDataControlSupportedTypeNames = _
  689.         "Data"
  690.     pvtCollectionSupportedTypeNames = _
  691.         "VBOFCollection"
  692.         
  693.     Set pvtDataControl = Nothing
  694. End Sub
  695.  
  696.  
  697. Private Sub Class_Terminate()
  698.     If Not ObjectManager Is Nothing Then
  699.     
  700. ' unregister the wrapper from the Form
  701. '       ObjectManager.pvtUnRegisterWrapperUnderForm _
  702.             Form:=Me.Form, _
  703.             Wrapper:=Me
  704.             
  705.         ObjectManager.TerminateObject _
  706.             Object:=Me
  707.     End If
  708. End Sub
  709.  
  710.